package cn.school.demo.ui.register

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import cn.school.demo.MainViewModel
import cn.school.demo.MyApplication
import cn.school.demo.R
import cn.school.demo.databinding.FragmentTeacherRegisterBinding
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.mylhyl.circledialog.CircleDialog

class RegisterTeacherFragment : Fragment() {
    private lateinit var mViewModel: RegisterViewModel
    private var _binding: FragmentTeacherRegisterBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mViewModel = ViewModelProvider(this).get(RegisterViewModel::class.java)
        _binding =
            DataBindingUtil.inflate(inflater, R.layout.fragment_teacher_register, container, false)
        binding.vm = mViewModel
        binding.view = this
        binding.lifecycleOwner = viewLifecycleOwner
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        init()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun init() {
        MyApplication.systemBarSetting(MyApplication.DARK, requireActivity())
        val array_colleges = MainViewModel().colleges
        val _adapter_colleges: ArrayAdapter<String> = ArrayAdapter(
            this.requireActivity(),
            R.layout.support_simple_spinner_dropdown_item,
            array_colleges
        )
        with(binding.registerCollegeTeacherSpinner) {
            adapter = _adapter_colleges
            setSelection(0)
            onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                override fun onItemSelected(
                    parent: AdapterView<*>?,
                    view: View?,
                    position: Int,
                    id: Long
                ) {
                    LogUtils.i("选中了${array_colleges[position]}")
                    //这里把选中的内容放到User实体里
                    mViewModel.setCollage(array_colleges[position])
                }

                override fun onNothingSelected(parent: AdapterView<*>?) {
                    ToastUtils.showShort("not implemented")
                }
            }
        }
    }

    fun registerTeacherGetCodeButtonOnClick() {
        val toastText = mViewModel.getVerificationCodeDetection()
        toastText?.let { ToastUtils.showShort(toastText) }
            ?: let {
                mViewModel.getCodeText(binding.registerTeacherGetCodeButton.text.toString())
                MyApplication.animationCountdown(
                    binding.registerTeacherGetCodeButton,
                    null,
                    "RegisterByTeacher"
                )
            }
    }

    fun buttonTeacherOnClick() {
        val toastText = mViewModel.getTeacherRegisterDetection()
        toastText?.let { ToastUtils.showShort(toastText) } ?: let {
            if (mViewModel.code.value == mViewModel.getCode.value) {
                mViewModel.setIdentity("教职工")
                mViewModel.getResult(mViewModel.user.value!!)
                mViewModel.result.observe(viewLifecycleOwner) {
                    LogUtils.d("我变化了啊！")
                    when (mViewModel.result.value?.success) {
                        true -> {
                            MyApplication.apply {
                                user = mViewModel
                                    .user.value
                                navigateThing["SetLove"] = "Article to SetLove"
                                goto(
                                    R.id.action_RegisterByTeacher_to_SetLove,
                                    findNavController(),
                                    parentFragmentManager
                                )
                            }
                        }
                        else -> CircleDialog.Builder()
                            .setText(mViewModel.result.value!!.message)
                            .setNegative("确定", null)
                            .show(parentFragmentManager)
                    }
                }
            } else {
                CircleDialog.Builder().setText("验证码错误！").setNegative("确定") {
                    mViewModel.setCode("")
                }.show(parentFragmentManager)
            }

        }
    }

    fun setId() = mViewModel.setId(binding.registerIDTeacherEditView.text.toString())
    fun setName() = mViewModel.setName(binding.registerNameTeacherEditView.text.toString())
    fun setPwd() = mViewModel.setPwd(binding.registerPasswordTeacherEditView.text.toString())
    fun setPhone() =
        mViewModel.setPhone(binding.registerPhoneTeacherEditView.text.toString())

    fun setRePwd() =
        mViewModel.setRePwd(binding.registerRePasswordTeacherEditView.text.toString())

    fun setCode() = mViewModel.setCode(binding.registerCodeTeacherEditView.text.toString())
}
